﻿using System.Web.Mvc;
using FlickrXplorer.Web.Models;
using FlickrXplorer.Web.Utilities;

namespace FlickrXplorer.Web.Controllers
{
    public class SecurityController : Controller
    {
        private readonly IPhotoModel model;
        private readonly ISecurityModel secModel;

        public SecurityController()
        {
            model = new PhotoModel();
            secModel = new SecurityModel();
        }

        [MvcAction]
        public RedirectResult Auth(string frob)
        {
            model.Authenticate();
            return RedirectToCurrentUrl();
        }

        public RedirectResult Logout()
        {
            if(secModel.IsAthenticated())
            {
                secModel.ClearToken();
            }
            return RedirectToCurrentUrl();
        }

        private RedirectResult RedirectToCurrentUrl()
        {
            string redirectUrl = Request.GetStoredUrl();
            //faiilsafe, invalid stored url.
            if (string.IsNullOrEmpty(redirectUrl))
                redirectUrl = "/Default.aspx";

            return Redirect(redirectUrl);
        }
    }
}
